<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Order extends Model
{

//    use SoftDelete;

    

    // 表名
    protected $name = 'order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'state'
    ];




    //根据paid和status获取订单状态
    public function getStateAttr($value,$data){
        if( $data['paid'] == 0 && !in_array($data['status'],[100,101,102,103]) ){
            return $value = -1;
        }elseif( $data['paid'] == 1 && $data['status'] == 0 ){
            return $value = 0;
        }elseif( $data['paid'] == 1 && in_array($data['status'],[1,2,3,4]) ){
            return $value = 1;
        }elseif( $data['paid'] == 1 && $data['status'] == 5 ){
            return $value = 2;
        }elseif( in_array($data['status'],[100,101,102,103]) ){
            return $value = 7;
        }
    }

    public function getReservedtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['reservedtime']) ? $data['reservedtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPaidtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paidtime']) ? $data['paidtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getExpiredtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['expiredtime']) ? $data['expiredtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCanceltimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['canceltime']) ? $data['canceltime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setReservedtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPaidtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setExpiredtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCanceltimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function steward()
    {
        return $this->belongsTo('Steward', 'steward_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function package()
    {
        return $this->belongsTo('app\admin\model\ServicePackage', 'service_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
